﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

// 磁铁
public class BuffMagnet : BuffBase
{
    float _radius = 4.5f;

    TimerMgr.Timer _magentTimer;

    public override void Init(int id, HumanCtrl owner, Action<int> endCallback)
    {
        base.Init(id, owner, endCallback);

        Start();
    }

    protected override void onStart()
    {
        base.onStart();

        _magentTimer = TimerMgr.Instance.CreateTimer(0, -1, onMagnetLoop);
        _magentTimer.Start();
    }

    // 吸金币和道具
    private void onMagnetLoop()
    {
        var allCollider = Physics2D.OverlapCircleAll(_owner.transform.position, _radius, 1<< LayerMask.NameToLayer("Friend"));
        // 只吸前面的，不吸后面的
        foreach (var collider in allCollider)
        {
            if (collider.transform.position.x < _owner.transform.position.x) { continue; }

            var coin = collider.gameObject.AddComponent<FlyTo>();
            if (coin != null)
            {
                coin.Target = _owner.transform;
            }
        }
    }

    protected override void onFinish()
    {
        base.onFinish();
        _magentTimer.Stop();
    }
}

